<template>
  <strong class="page-title">开票申请审核</strong>
  <el-tabs v-model="activeTab" class="custom-tabs">
   <el-tab-pane label="待审核" name="pending">
      <!-- 待审核内容 -->
      <el-card
        class="transaction-card"
        shadow="hover"
        style="position: relative; min-height: 800px"
      >
        <!-- 创建时间筛选区域，紧贴卡片左上角 -->
        <div class="date-picker-wrapper">
          <span class="date-picker-label">创建时间</span>
          <el-date-picker
            v-model="dateRange"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD HH:mm:ss"
            date-format="YYYY/MM/DD ddd"
            time-format="A hh:mm:ss"
            size="small"
            style="width: 250px"
          />
        </div>
        <el-table
          :data="tableData2"
          size="small"
          border
          :header-cell-style="{
            background: '#f8f8f9',
            color: '#333',
            fontWeight: 'bold',
          }"
        >
          <el-table-column
            v-for="col in columnsPending"
            :key="col.prop"
            :prop="col.prop"
            :label="col.label"
            :min-width="col.minWidth"
            :align="col.align"
          />
          <el-table-column label="操作" align="center" min-width="160">
            <template #default="scope">
              <el-button
                type="primary"
                size="small"
                @click="handleLook(scope.row)"
                >查看</el-button
              >
              <el-button
                type="danger"
                size="small"
                @click="openDialog(scope.row)"
                >处理</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div
          class="demo-pagination-block"
          style="position: absolute; right: 24px; bottom: 32px; margin-top: 0"
        >
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :size="size"
            :disabled="disabled"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </el-tab-pane>
    <el-tab-pane label="审核记录" name="record">
      <!-- 审核记录搜索区域 -->
      <el-card
        class="transaction-card"
        shadow="hover"
        style="position: relative; min-height: 420px"
      >
        <!-- 创建时间筛选区域，紧贴卡片左上角 -->
        <div class="date-picker-wrapper">
          <span class="date-picker-label">创建时间</span>
          <el-date-picker
            v-model="dateRange"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD HH:mm:ss"
            date-format="YYYY/MM/DD ddd"
            time-format="A hh:mm:ss"
            size="small"
            style="width: 250px"
          />
        </div>
        <!-- 审核记录内容，可根据实际需求替换下方内容 -->
        <el-table
          :data="tableData2"
          size="small"
          border
          :header-cell-style="{
            background: '#f8f8f9',
            color: '#333',
            fontWeight: 'bold',
          }"
        >
          <el-table-column
            v-for="col in columnsRecord"
            :key="col.prop"
            :prop="col.prop"
            :label="col.label"
            :min-width="col.minWidth"
            :align="col.align"
          />
          <el-table-column label="操作" align="center" min-width="100">
            <template #default="scope">
              <el-button
                type="primary"
                size="small"
                @click="handleLook(scope.row)"
                >查看</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div
          class="demo-pagination-block"
          style="position: absolute; right: 24px; bottom: 32px; margin-top: 0"
        >
          <br /><br />
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :size="size"
            :disabled="disabled"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </el-tab-pane>
  </el-tabs>

  
  <!-- 在模板底部添加el-dialog弹窗 -->
  <el-dialog
    v-model="dialogVisible"
    title="开票申请审核"
    width="800px"
    align-center
    @close="closeDialog"
  >
    <el-form
      :model="auditForm"
      :rules="auditRules"
      ref="auditFormRef"
      label-width="100px"
    >
      <!-- 第一行 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="票据类型">
            <span>{{ currentRow.name }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开票金额">
            <span style="color: #f00">￥{{ currentRow.no }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 第二行 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名">
            <span>{{ currentRow.phone }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人电话">
            <span>{{ currentRow.amount }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 第三行 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="收件人邮箱">
            <span>2121428724@qq.com</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="提交人">
            <span>陈添添</span>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 第四行 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="手机号">
            <span>{{ currentRow.amount }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <span>{{ currentRow.company }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 第五行 -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="提交时间">
            <span>{{ currentRow.type }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间">
            <span>{{ currentRow.date }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 开票活动占一整行 -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="开票活动">
            <div>
              <div>
                <span style="color: #409eff; cursor: pointer"
                  >普拉提系列-全身紧致</span
                >
                ￥666.00
              </div>
              <div>
                <span style="color: #409eff; cursor: pointer"
                  >普拉提系列-全身强化</span
                >
                ￥222.00
              </div>
            </div>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 审核结果占一整行 -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="审核结果" prop="auditResult">
            <el-radio-group v-model="auditForm.auditResult">
              <el-radio label="已开票">已开票</el-radio>
              <el-radio label="未通过">未通过</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 审核相关字段 -->
      <el-row v-if="auditForm.auditResult === '已开票'">
        <el-col :span="24">
          <el-form-item label="发票链接" prop="invoiceUrl">
            <el-input
              v-model="auditForm.invoiceUrl"
              placeholder="请输入发票链接"
            />
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              v-model="auditForm.invoiceRemark"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="auditForm.auditResult === '未通过'">
        <el-col :span="24">
          <el-form-item label="未通过原因" prop="failReason">
            <el-input
              v-model="auditForm.failReason"
              type="textarea"
              placeholder="请输入判定原因"
              maxlength="50"
              show-word-limit
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="onConfirm">确定</el-button>
    </template>
  </el-dialog>

  <!-- 在模板底部添加el-drawer抽屉 -->
  <el-drawer
    v-model="drawerVisible"
    title="开票申请详情"
    direction="rtl"
    size="600px"
  >
    <div style="padding: 0 16px">
      <div style="margin-bottom: 16px">
        <strong>票据类型：</strong>{{ detailRow.name || "-" }}
      </div>
      <div style="margin-bottom: 16px">
        <strong>开票金额：</strong>
        <span style="color: #f00">￥{{ detailRow.no || "-" }}</span>
      </div>
      <!-- 开票活动 -->
      <div style="margin-bottom: 16px">
        <strong>开票活动：</strong>
        <div v-if="detailRow.activities && detailRow.activities.length">
          <div
            v-for="(item, idx) in detailRow.activities"
            :key="idx"
            style="background: #f5f7fa; padding: 4px 8px; margin: 4px 0"
          >
            <span style="color: #409eff; cursor: pointer">{{ item.name }}</span>
            ￥{{ item.amount }}
          </div>
        </div>
        <div v-else>-</div>
      </div>
      <div style="margin-bottom: 8px">
        <strong>姓名：</strong>{{ detailRow.phone || "-" }}
      </div>
      <div style="margin-bottom: 8px">
        <strong>收件人电话：</strong>{{ detailRow.amount || "-" }}
      </div>
      <div style="margin-bottom: 8px">
        <strong>收件人邮箱：</strong>{{ detailRow.email || "-" }}
      </div>
      <div style="margin-bottom: 8px">
        <strong>提交人：</strong>{{ detailRow.phone || "-" }}
      </div>
      <div style="margin-bottom: 8px">
        <strong>手机号：</strong>{{ detailRow.amount || "-" }}
      </div>
      <!-- 状态区分 -->
      <div style="margin-bottom: 8px">
        <strong>状态：</strong>
        <template v-if="detailRow.company === '已开票'">
          <i style="color: #52c41a; font-size: 14px; vertical-align: middle"
            >●</i
          >
          已开票
        </template>
        <template v-else-if="detailRow.company === '未通过'">
          <i style="color: #ff4d4f; font-size: 14px; vertical-align: middle"
            >●</i
          >
          未通过
        </template>
        <template v-else>
          <i style="color: #faad14; font-size: 14px; vertical-align: middle"
            >●</i
          >
          {{ detailRow.company || "-" }}
        </template>
      </div>
      <!-- 已开票专属 -->
      <template v-if="detailRow.company === '已开票'">
        <div style="margin-bottom: 8px">
          <strong>发票链接：</strong>
          <span>{{ detailRow.invoiceUrl || "-" }}</span>
          <el-button
            v-if="detailRow.invoiceUrl"
            type="primary"
            link
            size="small"
            @click="copyText(detailRow.invoiceUrl)"
            >复制</el-button
          >
        </div>
        <div style="margin-bottom: 8px">
          <strong>开票时间：</strong>{{ detailRow.auditTime || "-" }}
        </div>
      </template>
      <!-- 未通过专属 -->
      <template v-else-if="detailRow.company === '未通过'">
        <div style="margin-bottom: 8px">
          <strong>未通过原因：</strong>{{ detailRow.failReason || "-" }}
        </div>
        <div style="margin-bottom: 8px">
          <strong>审核时间：</strong>{{ detailRow.auditTime || "-" }}
        </div>
      </template>
      <!-- 通用 -->
      <div style="margin-bottom: 8px">
        <strong>提交时间：</strong>{{ detailRow.type || "-" }}
      </div>
    </div>
  </el-drawer>
</template>

<script setup lang="ts">
// 组件名称定义
defineOptions({ name: "InvoiceApplicationReview" });
import { ref, nextTick } from "vue";
import { ElMessage } from "element-plus";
import type { FormInstance } from "element-plus";

// 定义表格行数据类型
interface TableRow {
  id: number;
  no: string;
  name: string;
  phone: string;
  amount: string;
  balance: number;
  company: string;
  type: string;
  date: string;
  activities?: Array<{ name: string; amount: number }>;
  email?: string;
  invoiceUrl?: string;
  failReason?: string;
  auditTime?: string;
}

// -------------------- Tab切换相关 --------------------
const activeTab = ref("pending");

// -------------------- 搜索与筛选相关 --------------------
const dateRange = ref<[Date, Date] | null>(null);

// -------------------- 分页相关 --------------------
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(300);
const size = ref("default");
const disabled = ref(false);
const background = ref(true);

// -------------------- 表格相关 --------------------
const columnsPending = [
  { prop: "no", label: "发票金额", minWidth: 90, align: "center" },
  { prop: "name", label: "票据类型", minWidth: 110, align: "center" },
  { prop: "phone", label: "提交人", minWidth: 100, align: "center" },
  { prop: "amount", label: "手机号", minWidth: 120, align: "right" },
  { prop: "balance", label: "余额(元)", minWidth: 100, align: "right" },
  { prop: "company", label: "状态", minWidth: 120, align: "center" },
  { prop: "type", label: "提交时间", minWidth: 140, align: "center" },
  { prop: "date", label: "创建时间", minWidth: 160, align: "center" },
];

const columnsRecord = [
  { prop: "no", label: "发票金额", minWidth: 90, align: "center" },
  { prop: "name", label: "票据类型", minWidth: 110, align: "center" },
  { prop: "company", label: "状态", minWidth: 120, align: "center" },
  { prop: "type", label: "提交时间", minWidth: 140, align: "center" },
];

const tableData2 = ref<TableRow[]>([
  {
    id: 1,
    no: "1000",
    name: "增值税专用发票",
    phone: "张三",
    amount: "13800000000",
    balance: 1000,
    company: "待审核",
    type: "2024-06-01 10:00:00",
    date: "2024-06-01 09:00:00",
    activities: [
      { name: "普拉提系列-全身紧致", amount: 666 },
      { name: "普拉提系列-全身强化", amount: 222 },
    ],
    email: "2121428724@qq.com",
    invoiceUrl: "https://xxx.com/invoice/1",
    failReason: "",
    auditTime: "2024-06-01 12:00:00",
  },
  {
    id: 2,
    no: "2000",
    name: "普通发票",
    phone: "李四",
    amount: "13900000000",
    balance: 2000,
    company: "已开票",
    type: "2024-06-02 11:00:00",
    date: "2024-06-02 10:00:00",
    activities: [
      { name: "普拉提系列-全身紧致", amount: 666 },
      { name: "普拉提系列-全身强化", amount: 222 },
    ],
    email: "2121428724@qq.com",
    invoiceUrl: "https://xxx.com/invoice/2",
    failReason: "",
    auditTime: "2024-06-02 13:00:00",
  },
  {
    id: 3,
    no: "3000",
    name: "普通发票",
    phone: "王五",
    amount: "13700000000",
    balance: 3000,
    company: "未通过",
    type: "2024-06-03 11:00:00",
    date: "2024-06-03 10:00:00",
    activities: [],
    email: "2121428724@qq.com",
    invoiceUrl: "",
    failReason: "资料不全",
    auditTime: "2024-06-03 13:00:00",
  },
]);

// 分页处理函数
const handleSizeChange = (val: number) => {
  pageSize.value = val;
};

const handleCurrentChange = (val: number) => {
  currentPage.value = val;
};

// -------------------- 审核弹窗相关 --------------------
const dialogVisible = ref(false);
const auditFormRef = ref<FormInstance>();
const currentRow = ref<TableRow>({} as TableRow);

const auditRules = {
  auditResult: [
    { required: true, message: "请选择审核结果", trigger: "change" },
  ],
  invoiceUrl: [{ required: false, message: "请输入发票链接", trigger: "blur" }],
  failReason: [
    { required: false, message: "请输入未通过原因", trigger: "blur" },
  ],
};

const auditForm = ref({
  auditResult: "已开票",
  invoiceUrl: "",
  failReason: "",
  invoiceRemark: "",
});

const openDialog = (row: TableRow) => {
  currentRow.value = { ...row };
  dialogVisible.value = true;
  auditForm.value = {
    auditResult: "已开票",
    invoiceUrl: "",
    failReason: "",
    invoiceRemark: "",
  };
  nextTick(() => {
    resetForm(auditFormRef.value);
  });
};

const resetForm = (formRef: FormInstance | undefined) => {
  if (!formRef) return;
  formRef.resetFields();
};

const onConfirm = () => {
  if (!auditFormRef.value) return;
  auditFormRef.value.validate((valid) => {
    if (valid) {
      dialogVisible.value = false;
      auditForm.value.invoiceRemark = "";
      if (auditForm.value.auditResult === "已开票") {
        ElMessage.success("该笔申请已开票，请通知用户");
      } else if (auditForm.value.auditResult === "未通过") {
        ElMessage.error("该笔申请未通过审核失败，请重新尝试");
      }
      resetForm(auditFormRef.value);
    }
  });
};

const closeDialog = () => {
  dialogVisible.value = false;
  resetForm(auditFormRef.value);
};

// -------------------- 详情抽屉相关 --------------------
const drawerVisible = ref(false);
const detailRow = ref<TableRow>({} as TableRow);

const handleLook = (row: TableRow) => {
  detailRow.value = { ...row };
  drawerVisible.value = true;
};

const copyText = (text: string) => {
  navigator.clipboard.writeText(text);
  ElMessage.success("复制成功");
};
</script>

<style scoped>
.solid-border {
  border: 2px solid hsl(206, 22%, 86%); /* 边框宽度、样式、颜色 */
  padding: 5px 10px; /* 内边距，控制文字与边框的间距 */
  border-radius: 5px; /* 圆角半径，调整这个值来改变圆角的弧度 */
  display: inline-block; /* 使边框包裹文字，并允许与其他元素并排 */
}
.date-picker-wrapper {
  display: flex;
  align-items: center;
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  height: 40px;
  width: 360px;
  padding: 0;
  box-sizing: border-box;
  overflow: hidden;
}
.date-picker-label {
  background: #fff;
  border: none;
  border-right: 1px solid #ebeef5;
  border-radius: 6px 0 0 6px;
  height: 100%;
  padding: 0 16px;
  color: #606266;
  font-size: 14px;
  display: flex;
  align-items: center;
  font-weight: 500;
  white-space: nowrap;
}
.date-picker-wrapper .el-date-editor,
.date-picker-wrapper .el-input__wrapper,
.date-picker-wrapper .el-input__inner {
  border: none !important;
  box-shadow: none !important;
  background: #fff !important;
}
.date-picker-wrapper .el-date-editor.is-active,
.date-picker-wrapper .el-date-editor:focus,
.date-picker-wrapper .el-input__wrapper.is-focus,
.date-picker-wrapper .el-input__wrapper:focus {
  border: none !important;
  box-shadow: none !important;
}

/* 卡片样式优化 */
.transaction-card {
  width: 95% !important;
  margin: 0 auto !important;
  border-radius: 16px !important;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.03) !important;
  padding: 32px 24px 24px 24px !important;
  background: #fff !important;
  border: none !important;
}

/* 筛选区紧贴卡片左上角 */
.date-picker-wrapper {
  margin-top: -8px;
  margin-bottom: 8px;
  margin-left: 0;
}

/* 表格整体去线条，内容单行省略号 */
.el-table {
  margin-bottom: 8px !important; /* 表格与分页栏间距 */
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
}
.el-table__header-wrapper thead tr {
  background: #f8f8f9 !important;
  border-radius: 8px !important;
}
.el-table__header-wrapper th {
  background: transparent !important;
  border: none !important;
  border-radius: 0 !important;
  color: #333 !important;
  font-weight: bold !important;
  padding: 12px 0 !important;
}
.el-table__body-wrapper td {
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
  padding: 10px 0 !important;
  white-space: nowrap !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
  max-width: 180px;
}
.el-table__row {
  background: #fff !important;
}
.el-table__inner-wrapper::before,
.el-table__border-left-patch,
.el-table__body-wrapper .el-table__cell.is-last-column {
  background: #fff !important;
  border: none !important;
  content: none !important;
  display: none !important;
}
.el-table__cell {
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
  white-space: nowrap !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
}

/* 分页栏居中，紧贴卡片底部 */
.demo-pagination-block {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: static !important;
  margin-top: 8px !important;
  margin-bottom: 0 !important;
}

.custom-tabs {
  margin-left: 56px;
}
.page-title {
  display: block;
  font-size: 25px;
  margin-left: 56px;
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: bold;
}
.demo-pagination-block {
  width: auto;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  position: absolute !important;
  right: 24px;
  bottom: 24px;
  margin: 0 !important;
}
</style>
